extends Node

@export var valve_cold: InteractiveObject3D
@export var valve_hot: InteractiveObject3D
@export var water_emmiter: GPUParticles3D
@export var steam_emmiter: GPUParticles3D
@export var amount_per_flow = 16

var flow_strength = 0
var temperature = 0

func _ready() -> void:
	valve_cold.interaction_begin.connect(cold_begin)
	valve_cold.interaction_end.connect(cold_end)
	valve_hot.interaction_begin.connect(hot_begin)
	valve_hot.interaction_end.connect(hot_end)

func cold_begin(params: Dictionary) -> void:
	flow_strength += params['state']
	recalculate_flow()

func cold_end(params: Dictionary) -> void:
	flow_strength += params['state']
	recalculate_flow()

func hot_begin(params: Dictionary) -> void:
	flow_strength += params['state']
	temperature += params['state']
	recalculate_flow()

func hot_end(params: Dictionary) -> void:
	flow_strength += params['state']
	temperature += params['state']
	recalculate_flow()

func recalculate_flow() -> void:
	water_emmiter.emitting = flow_strength > 0
	if water_emmiter.emitting:
		water_emmiter.amount = amount_per_flow * flow_strength
	steam_emmiter.emitting = temperature > flow_strength / 2
	if steam_emmiter.emitting:
		steam_emmiter.amount = amount_per_flow * temperature
